/* 1videoConference -- An open source video conferencing platform.
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Data;
//using _videoConference.User;
using Adiance.VMukti.BAL;


namespace ZipInfo
{
    public class CliZipInfoMain
    {
        NetPeerService.NetPeerServer ZipInfoMainServer = null;
        NetPeerService.NetPeerClient npcZipInfoMain = null;
        object objZipInfoMain = new CsvcZipInfoMain();
        IsvcZipInfoMain ZipInfoMain;

        System.Threading.Thread servClient;
        List<CliZipInfoSub> MySubUsers = new List<CliZipInfoSub>();
        int UserCounter = 1;

        public CliZipInfoMain()
        {
            servClient = new System.Threading.Thread(new System.Threading.ThreadStart(RegClient));
            try
            {
                ZipInfoMainServer = new NetPeerService.NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/ZipInfo");
                ZipInfoMainServer.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/ZipInfo");
                ZipInfoMainServer.OpenServer();
                servClient.Start();
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--CliZipInfoMain.cs--:--CliZipInfoMain()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //MessageBox.Show("CliZipInfoMain" + exp.Message);
            }
        }

        void RegClient()
        {
            lock (this)
            {
                try
                {
                    ((CsvcZipInfoMain)objZipInfoMain).entsvcJoin += new CsvcZipInfoMain.delsvcJoin(CliZipInfo_entsvcJoin);
                    ((CsvcZipInfoMain)objZipInfoMain).entsvcStarted += new CsvcZipInfoMain.delsvcServiceStarted(CliZipInfo_entsvcStarted);
                    ((CsvcZipInfoMain)objZipInfoMain).entsvcUnJoin += new CsvcZipInfoMain.delsvcUnJoin(CliZipInfoMain_entsvcUnJoin);

                    npcZipInfoMain = new NetPeerService.NetPeerClient();
                    ZipInfoMain = (IsvcZipInfoMain)npcZipInfoMain.OpenClient<IsvcZipInfoMain>("net.tcp://" + UserInfo.strMachIP + ":2500/ZipInfo", "ZipInfoMesh", ref objZipInfoMain);
                    ZipInfoMain.svcJoin("Server",UserInfo.MeetingID);
                }
                catch (Exception exp)
                {
                    //MessageBox.Show("RegClient-ZipInfoMain" + exp.Message);
                    exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--CliZipInfoMain.cs--:--RegClient()--:--");
                    Adiance.VMukti.BAL.clsUtility.LogException = exp;
                }
            }
        }

        void CliZipInfo_entsvcJoin(string uName, string MeetingID)
        {
            lock (this)
            {
                try
                {
                    if (uName != "Server")
                    {
                        MySubUsers.Add(new CliZipInfoSub("net.tcp://" + UserInfo.strMachIP + ":2500/ZipInfo/" + UserCounter.ToString(), uName, MeetingID));
                        ZipInfoMain.svcServiceStarted(uName, "net.tcp://" + UserInfo.strMachIP + ":2500/ZipInfo/" + UserCounter.ToString());
                        UserCounter++;
                    }
                }
                catch (Exception ex)
                {
                    ex.Data.Add("My Key", "Adiance.VMukti.BAL--:--CliZipInfoMain.cs--:--CliZipInfo_entsvcJoin()--:--");
                    Adiance.VMukti.BAL.clsUtility.LogException = ex;
                    //MessageBox.Show(ex.Message);
                }
            }
        }

        void CliZipInfo_entsvcStarted(string uName, string URI)
        {
        }

        void CliZipInfoMain_entsvcUnJoin(string uName)
        {
            for (int i = 0; i < MySubUsers.Count; i++)
            {
                if (MySubUsers[i].UserName == uName)
                {
                    MySubUsers[i].CloseService();
                    MySubUsers.RemoveAt(i);
                    break;
                }
            }
        }

        public void CloseService()
        {
            ZipInfoMain.svcUnJoin("Server");
            npcZipInfoMain.CloseClient<IsvcZipInfoMain>();
            ZipInfoMainServer.CloseServer();
        }
    }
}
